<?php
session_start();
require_once 'config.php';
require_once 'login_check.php';
check_admin_session();

$change_msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password'])) {
    $oldpwd = $_POST['oldpwd'];
    $newpwd = $_POST['newpwd'];
    $newpwd2 = $_POST['newpwd2'];
    $username = $_SESSION['admin_username'];
    
    if ($newpwd !== $newpwd2) {
        $change_msg = '新密码两次输入不一致';
    } elseif (strlen($newpwd) < 6) {
        $change_msg = '新密码长度至少6位';
    } else {
        $stmt = $pdo->prepare("SELECT password FROM admin WHERE username=? LIMIT 1");
        $stmt->execute([$username]);
        $row = $stmt->fetch();
        
        if ($row && password_verify($oldpwd, $row['password'])) {
            $new_hash = password_hash($newpwd, PASSWORD_DEFAULT);
            $stmt2 = $pdo->prepare("UPDATE admin SET password=? WHERE username=?");
            if ($stmt2->execute([$new_hash, $username])) {
                $change_msg = '密码修改成功';
            } else {
                $change_msg = '密码修改失败';
            }
        } else {
            $change_msg = '原密码错误';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码</title>
    <!-- 引入图标库保持风格统一 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@7.4.47/css/materialdesignicons.min.css">
    <style>
        /* 基础样式与登录页保持一致 */
        body {
            font-family: 'Inter', 'Segoe UI', 'Helvetica Neue', Arial, 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: #f5f7fa;
            margin: 0;
            padding: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* 容器样式参考登录页卡片设计 */
        .manage-container {
            width: 100%;
            max-width: 380px;
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 4px 18px rgba(41, 128, 255, 0.08);
            padding: 40px 32px;
            display: flex;
            flex-direction: column;
            align-items: center;
            transition: box-shadow 0.2s;
        }

        .manage-container:hover {
            box-shadow: 0 8px 24px rgba(41, 128, 255, 0.12);
        }

        /* 标题样式与登录页统一 */
        .manage-title {
            font-size: 1.8em;
            font-weight: bold;
            margin-bottom: 32px;
            color: #2980ff;
            letter-spacing: 1.5px;
            display: flex;
            align-items: center;
        }

        .manage-title .mdi {
            font-size: 1.5em;
            margin-right: 12px;
        }

        /* 表单区域居中布局 */
        .change-pwd-form {
            width: 100%;
            margin-top: 16px;
        }

        .change-pwd-title {
            font-size: 1.1em;
            color: #2980ff;
            margin-bottom: 16px;
            text-align: center;
        }

        /* 输入框样式与登录页保持一致 */
        .change-pwd-form input {
            width: 100%;
            font-size: 1em;
            padding: 12px 14px;
            margin: 12px 0;
            border-radius: 6px;
            border: 1px solid #ddd;
            box-sizing: border-box;
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        .change-pwd-form input:focus {
            outline: none;
            border-color: #2980ff;
            box-shadow: 0 0 0 2px rgba(41, 128, 255, 0.2);
        }

        /* 按钮样式统一化 */
        .change-pwd-btn {
            width: 100%;
            padding: 10px 0;
            font-size: 1.1em;
            background: #2980ff;
            color: #fff;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            transition: background 0.2s, box-shadow 0.2s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            margin-top: 16px;
        }

        .change-pwd-btn .mdi {
            font-size: 1.2em;
        }

        .change-pwd-btn:hover {
            background: #1866d1;
            box-shadow: 0 2px 8px rgba(41, 128, 255, 0.3);
        }

        /* 消息提示样式 */
        .msg {
            color: #ff4d4f;
            margin: 0 0 16px 0;
            text-align: center;
            font-size: 0.95em;
            padding: 8px;
            border-radius: 4px;
        }

        .msg.success {
            color: #43b362;
            background-color: #f0fff4;
        }

        /* 退出按钮样式调整 */
        .logout-btn {
            width: 100%;
            padding: 10px 0;
            font-size: 1.1em;
            background: #f5f5f5;
            color: #666;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.2s;
            margin-top: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .logout-btn .mdi {
            font-size: 1.2em;
        }

        .logout-btn:hover {
            background: #e8e8e8;
            color: #ff4d4f;
        }

        /* 响应式适配 */
        @media (max-width: 600px) {
            .manage-container {
                max-width: 90vw;
                padding: 24px 16px;
            }

            .manage-title {
                font-size: 1.5em;
                margin-bottom: 24px;
            }
        }
    </style>
</head>
<body>
    <div class="manage-container">
        <div class="manage-title">
            <span class="mdi mdi-key"></span>修改密码
        </div>
        
        <form class="change-pwd-form" method="post" autocomplete="off">
            <?php if ($change_msg): ?>
                <div class="msg<?php echo ($change_msg === '密码修改成功') ? ' success' : ''; ?>">
                    <?php echo htmlspecialchars($change_msg); ?>
                </div>
            <?php endif; ?>
            
            <input type="password" name="oldpwd" placeholder="原密码" required>
            <input type="password" name="newpwd" placeholder="新密码" required>
            <input type="password" name="newpwd2" placeholder="确认新密码" required>
            
            <button class="change-pwd-btn" type="submit" name="change_password" value="1">
                <span class="mdi mdi-refresh"></span>修改密码
            </button>
            <button class="change-pwd-btn" type="button" class="return-btn" onclick="window.location.href='announcement_manage.php'">
                <span class="mdi mdi-arrow-left"></span>返回管理页
            </button>
        </form>
        
        
    </div>
</body>
</html>